package com.example.minato.minastore.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.example.minato.minastore.down.DownInfo;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import okhttp3.ResponseBody;

/**
 * Created by minato on 2018/7/21.
 * 常量池
 */

public class AppUtils {

    public static final String DB_NAME = "rong_db";
    public static final String DB_NAME_NET = "rong_net_db";
    public static final String URL_APK_UPDATE = "versionMng/lastVersion";//升级地址


//        public static String BASE_URL=" http://10.2.91.17:8021/";
//        public static String BASE_URL=" http://10.2.91.14:8021/";


    public static String BASE_URL="https://nm.rongrong.cn/";                              //线上环境
    public static String COURSE_IMAGE_URL = "https://video.rongrong.cn/uploadFile/onlinestudy/";   //培训图片服务器
    public static String BASE_URL_H5 = "https://m.rongrong.cn/";

//    public static String BASE_URL="http://nm.rongrong.huazhi365.com/";  //测试环境
//    public static String COURSE_IMAGE_URL = "http://video.rongrong.huazhi365.com/uploadFile/onlinestudy/";
//    public static String BASE_URL_H5 = "http://m.rr.huazhi365.com/";  //h5手机页面



    public static String COURSE_VIDEO_URL = "https://video.rongrong.cn/uploadFile/onlinestudy";
    public static String IMG2_URL = "http://image2.rongrong.cn/";  //标准图片服务器



    /**
     * 判断网络是否有效.
     */
    public static boolean isNetworkAvailable(Context context) {
        try {
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo info = connectivity.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 读取base url
     */
    public static String getBaseUrl(String url) {
        String head = "";
        int index = url.indexOf("://");
        if (index != -1) {
            head = url.substring(0, index + 3);
            url = url.substring(index + 3);
        }
        index = url.indexOf("/");
        if (index != -1) {
            url = url.substring(0, index + 1);
        }
        return head + url;
    }

    /**
     * 写入文件
     */
    public static void writeCache(ResponseBody responseBody, File file, DownInfo info) throws IOException {
        if (!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        long allLength;
        if (info.getCountLength() == 0) {
            allLength = responseBody.contentLength();
        } else {
            allLength = info.getCountLength();
        }
        FileChannel channelOut = null;
        //文件角标操作
        RandomAccessFile randomAccessFile = null;
        randomAccessFile = new RandomAccessFile(file, "rwd");
        channelOut = randomAccessFile.getChannel();

        MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE,
                info.getReadLength(), allLength - info.getReadLength());

        byte[] buffer = new byte[1024 * 8];
        int len;
        int record = 0;
        while ((len = responseBody.byteStream().read(buffer)) != -1) {
            mappedBuffer.put(buffer, 0, len);
            record += len;
        }
        responseBody.byteStream().close();
        if (channelOut != null) {
            channelOut.close();
        }
        if (randomAccessFile != null) {
            randomAccessFile.close();
        }
    }


}
